package world0.gui;

import java.util.ArrayList;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.geom.Polygon;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.geom.Shape;

import world0.Player;
import world0.Skill;
import world0.items.Item;
import world0.util.Position;

public class UserInterface {
	
	private Player player;
	private String lives = "0";
	
	private boolean dmgIn;
	
	private int yDmg = 25;
	private int yHeal = 25;
	
	private int mouseX;
	private int mouseY;
	
	private int worldX;
	private int worldY;
	
	private int minutes = 0;
	private int m = -10;
	
	private boolean isDebug = false;
	private boolean isHelp = false;
	private boolean isCharWindow = false;
	
	private int itemGrabIndex;
	private boolean isItemGrabed;
	
	private Container container;
	private ArrayList<Item> itemList;
	
	private Polygon nameBackground;
	
	public UserInterface(Player player, GameContainer gc) {
		this.player = player;
		/* Container Name, xposition, yposition, xwidth, yheight */
		container = new Container("Inventar", Position.screenCenterX(500), 
										Position.screenButtomY(50), 500, 50);
		container.setColor(Color.gray);
		container.addItemSlot(9);
	}
	
	public void draw(Graphics g, GameContainer gc) {	
		
		container.draw(g);
		
		for(int i = 0; i < itemList.size(); i++) {
			itemList.get(i).draw(g);
		}
		
		g.setColor(Color.black);
		/* Keys */
		if(isHelp) {
			g.drawString("Quado: Left-clic", 25, gc.getHeight() - 25);
			g.drawString("Crates: Right-clic", 25, gc.getHeight() - 45);
			g.drawString("Moving: W A S D", 25, gc.getHeight() - 65);
			g.drawString("Exp up: L + 1/2/3", 25, gc.getHeight() - 85);
			g.drawString("Debug: F12", 25, gc.getHeight() - 105);
		} else {
			g.drawString("F1 for help", 25, gc.getHeight() - 25);
		}
		
		/* Life */	
		lives = String.valueOf(player.getLive());
		g.drawString("Lives: " + lives, (gc.getWidth() - 220), 35);

		
		/* EXP - Level */
		if(isCharWindow) {
			String sword = String.valueOf(player.skill.getSwordSkill());
			String twoHandSword = String.valueOf(player.skill.getTwoHandSwordSkill());
			String twoOneHandSword = String.valueOf(player.skill.getTwoOneHandSword());
			g.drawString("Sword EXP: " + sword, (gc.getWidth() - 220), 55);
			g.drawString("TwoHandSword EXP: " + twoHandSword, (gc.getWidth() - 220), 75);
			g.drawString("TwoOneHandSword EXP: " + twoOneHandSword, (gc.getWidth() - 220), 95);
		} else {
			g.drawString("'I' for Skills", gc.getWidth() - 220, 55);
		}
		
		/* Combattext */ /* Take DMG */
		/*
		g.translate(-player.getX() + gc.getWidth()/2, -player.getY() + gc.getHeight()/2);
		if(player.isDmgIn()) {
			yDmg += 1;
			g.setColor(Color.red);
			g.drawString("-1", player.getX() - 35, player.getY() - 80 + yDmg);
			
			if(yDmg >= 50) {
				player.setDmgIn(false);
				yDmg = 25;
			}
		}
		
		
		/* Heal 
		if(player.isHeal()) {
			yHeal -= 1;
			g.setColor(Color.green);
			g.drawString("+1", player.getX() + 15, player.getY() - 50 + yHeal);
			
			if(yHeal <= 0) {
				player.setHeal(false);
				yHeal = 25;
			}
		}
		g.resetTransform();
		*/
		
		/* Gameover Screen - TODO make a real State...  */
		if(player.getLive() <= 0) {
			Rectangle rectangel = new Rectangle(0, 0, gc.getWidth() + 5, gc.getHeight() + 5);
			g.setColor(Color.black);
			g.fill(rectangel);
			g.setColor(Color.red);
			g.drawString("### YOU LOSE ###", (gc.getWidth() /2), gc.getHeight() /2);
			g.setColor(Color.darkGray);
			g.drawString("or press `R`", (gc.getWidth() /2), (gc.getHeight() /2) + 20);
		}
		
		/* Show over item Name */
		for(int i = 0; i < itemList.size(); i++) {
			if(itemList.get(i).isHit(mouseX, mouseY)) {
				String string = itemList.get(i).getName();
				showBackground(mouseX + 15, mouseY - 21, g.getFont().getWidth(string) + 10, 
						g.getFont().getHeight(string) + 5, g);
				g.setColor(Color.magenta);
				g.drawString(string, mouseX + 20, mouseY - 20);
				break;
			}
		}
		
		/* Debug */
		if(isDebug) {
			g.setColor(Color.black);
			g.drawString("World-Zero Version: 0.01", (gc.getWidth() - 290), (gc.getHeight() - 25));
			g.drawString("Player position X: " + (int)player.getX() + " Y: " + (int)player.getY(), (gc.getWidth() - 290), (gc.getHeight() - 65));
			g.drawString("Old X: " + itemList.get(itemGrabIndex).getResetX(), (gc.getWidth() - 290), (gc.getHeight() - 85));
			g.drawString("x: " + worldX + " y: " + worldY, mouseX + 20, mouseY - 20);
			
			//int FPS = gc.getFPS();
			//g.drawString("FPS: " + Integer.toString(FPS) + " Objects " + (player.getSolidListSize() + player.getSlopeListSize()) + " " + "Mobs: " + player.getMobListSize(), (gc.getWidth() - 290), (gc.getHeight() - 45));
			//g.drawString("Objects " + (player.getSolidListSize() + player.getSlopeListSize()) + " " + "Mobs: " + player.getMobListSize(), (gc.getWidth() - 290), (gc.getHeight() - 85));
			//g.drawString("Delta: " + delta, (gc.getWidth() - 290), (gc.getHeight() - 105));
			//g.drawString("isJump: " + player.isJump() + " isFall: " + player.isFall(), (gc.getWidth() - 290), (gc.getHeight() - 205));
			//g.drawString("Secounds: " + System.nanoTime() / 1000000000 + " Minuten: " + (System.nanoTime() / 1000000000) / 60, (gc.getWidth() - 590), (gc.getHeight() - 125));
			//g.setColor(Color.black);
		}
		
	}	/* draw() END */
 
	public void update(GameContainer gc,  float delta) {
		
		m += 0.2 * delta;
		minutes = m / 60;
		
		Input input = gc.getInput();

		mouseX = input.getMouseX();
		mouseY = input.getMouseY();
		
		int cameraX = (int)player.getX() - (gc.getWidth() / 2);
		worldX = cameraX + mouseX;
		int cameraY = (int)player.getY() - (gc.getHeight() / 2);
		worldY = cameraY + mouseY;
		
		
		lives = String.valueOf(player.getLive());
		
		/* item move */
		if(input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) && !isItemGrabed) {
			for(int i = 0; i < itemList.size(); i++) {
				if(itemList.get(i).isHit(mouseX, mouseY)) {
					//System.out.println(itemList.get(i).getName());
					itemGrabIndex = i;
					isItemGrabed = true;
				}
			}
		}
		
		if(input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) && isItemGrabed) {
			itemList.add(0, itemList.get(itemGrabIndex));
			itemList.remove(itemGrabIndex);
			itemGrabIndex = 0;
			itemList.get(itemGrabIndex).setSize(46, 46);
			itemList.get(itemGrabIndex).setPosition(mouseX - 23, mouseY - 23);//(mouseX-40, mouseY-40);
			
		} else if(isItemGrabed){
			/* reset if no slot */
			if(!container.isSlotPlace(itemList.get(itemGrabIndex), mouseX, mouseY)) {
				itemList.get(itemGrabIndex).toOldPosition();
				isItemGrabed = false;
			} else {
				System.out.println("new Slot: " + itemList.get(itemGrabIndex).getResetX());
			/* place to new slot */
				//itemList.get(itemGrabIndex).setPlace(itemList.get(itemGrabIndex).getPositionsSize()))
				itemList.get(itemGrabIndex).setPosition(container.getFreeX(itemList.get(itemGrabIndex), mouseX, mouseY), container.getFreeY(itemList.get(itemGrabIndex), mouseX, mouseY));
				//itemList.get(itemGrabIndex).setPosition(container.getSlotX(itemList.get(itemGrabIndex)), container.getSlotY(itemList.get(itemGrabIndex)));
				itemList.get(itemGrabIndex).setPositionResetX(container.getFreeX(itemList.get(itemGrabIndex), mouseX, mouseY), container.getSlotWidth(itemList.get(itemGrabIndex)));
				itemList.get(itemGrabIndex).setPositionResetY(container.getFreeY(itemList.get(itemGrabIndex), mouseX, mouseY), container.getSlotHeight(itemList.get(itemGrabIndex)));
				isItemGrabed = false;
			}
		}

		if(input.isKeyDown(Input.KEY_F12)) {
			isDebug = true;
		} else {
			isDebug = false;
		}
		
		if(input.isKeyDown(Input.KEY_F1)) {
			isHelp = true;
		} else {
			isHelp = false;
		}
		
		if(input.isKeyDown(Input.KEY_I)) {
			isCharWindow = true;
		} else {
			isCharWindow = false;
		}
	}

	private void showBackground(int x, int y, int width, int height, Graphics g) {
		nameBackground = new Polygon();
		nameBackground.addPoint(0, 0);
		nameBackground.addPoint(width, 0);
		nameBackground.addPoint(width, height);
		nameBackground.addPoint(0, height);
		nameBackground.setLocation(x, y);
		g.setColor(Color.black);
		g.fill(nameBackground);
		nameBackground = null;
	}
	
	public void placeItemToFreeSlot(Item item) {
		item.setPosition(container.getFreeItemSlotXPosition(item), container.getFreeItemSlotYPosition(item));
	}
	
	public void setItemList(ArrayList<Item> itemList) {
		this.itemList = itemList;
	}
	
	public boolean isHit() {
		boolean isHit = false;
		if(container.area.contains(mouseX, mouseY)) {
			isHit = true;
		}
		return isHit;
	}
}
